package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.jjy.duss.yy.Z_IF_OPS009;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-01-14
 * @Description: 档期选品申请流程回传SAP
 * @ModifyLog:
 **/

public class WeaYYdqxpAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用Action: WeaYYdqxpAction");
        User user = requestInfo.getRequestManager().getUser();
        int userid = user.getUID();
        String currentDate = requestInfo.getRequestManager().getCurrentDate().replaceAll("-","");//审批日期
        String currentTime = requestInfo.getRequestManager().getCurrentTime().replaceAll(":","");//审批时间
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        List<Map<String, String>> list = new ArrayList<>();
        //获取主表
        rs.executeQuery("select id, DQXPNO,SJDQNO,spjg from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("select id, DQXPNO,SJDQNO,spjg from " + tablename + " where requestid=?", requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));//外键
        String DQXPNO = Util.null2String(rs.getString("DQXPNO"));//选品单号
        String SJDQNO = Util.null2String(rs.getString("SJDQNO"));//实际档期编码
        String REL_FLAG = Util.null2String(rs.getString("spjg"));//审批标记
        rs2.executeQuery("select workcode from hrmresource  where id=?",userid);
        rs2.next();
        baseBean.writeLog("select workcode from hrmresource  where id=?",userid);
        String PERNR = Util.null2String(rs2.getString("workcode"));//审批人
        Map<String, String> map = new HashMap();
        map.put("DQXPNO", DQXPNO); //选品单号
        map.put("SJDQNO", SJDQNO); //实际档期编号
        map.put("REL_FLAG", REL_FLAG);   //审批标记
        map.put("REL_USER", PERNR);   //审批人
        map.put("REL_DATUM", currentDate);  //审批日期
        map.put("REL_UZEIT", currentTime);  //审批时间
        baseBean.writeLog("相关参数:" + map.toString());
        //获取明细表
        String sqlmx = "select *  from " + tablename + "_dt2 where mainid='" + mainid + "'";
        rs2.executeQuery(sqlmx);
        while (rs2.next()) {
            String DQXPNO1 = Util.null2String(rs2.getString("DQXPNO"));       //选品单号
            String DQXPLN = Util.null2String(rs2.getString("DQXPLN"));       //选品序号
            String MATNR = Util.null2String(rs2.getString("MATNR"));       //商品编码
            String SPBZ = Util.null2String(rs2.getString("SPBZ"));         //审批备注
            Map<String, String> map2 = new HashMap();
            map2.put("DQXPNO", DQXPNO1);
            map2.put("DQXPLN", DQXPLN);
            map2.put("MATNR", MATNR);
            map2.put("SPBZ", SPBZ);
            list.add(map2);
            baseBean.writeLog("明细表相关参数:" + list.toString());
        }
        try{
            Z_IF_OPS009 if_ops009 = new Z_IF_OPS009();
            Map resultMap = if_ops009.execute(map,list);
            String flag = (String) resultMap.get("flag");
            String message = (String) resultMap.get("message");
            baseBean.writeLog("SAP返回结果——flag:" + flag + ";message:" + message);
            if("s".equalsIgnoreCase(flag)){
                return Action.SUCCESS;
            }else {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
